package com.smartlifev30.baiwei.activity;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;


import com.smartlifev30.R;
import com.smartlifev30.baiwei.adapter.MainFragmentAdapter;
import com.smartlifev30.baiwei.fragment.HomeFragment;
import com.smartlifev30.baiwei.fragment.MessageFragment;
import com.smartlifev30.baiwei.fragment.MineFragment;
import com.smartlifev30.mvp.module.MainActivityModule;
import com.smartlifev30.mvp.presenter.MainActivityPresent;
import com.smartlifev30.mvp.view.IMainActivityView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主页面
 *
 * @author uesr
 */
public class MainActivity extends BaseActivity<MainActivityPresent> implements IMainActivityView {

    @BindView(R.id.main_viewpager)
    ViewPager viewPager;

    @BindView(R.id.main_tab_layout)
    TabLayout tabLayout;

    /**
     * 未读消息数
     */
    TextView msgCount;

    private MainFragmentAdapter mainFragmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected MainActivityPresent createPresent() {
        return new MainActivityPresent(this, new MainActivityModule());
    }

    private void initData() {

    }


    private void initView() {
        ArrayList<Fragment> data = new ArrayList<>();
        data.add(new HomeFragment());
        data.add(new MessageFragment());
        data.add(new MineFragment());
        mainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager(), data);
        viewPager.setAdapter(mainFragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
        //初始化底部table
        for (int i = 0, tabCount = tabLayout.getTabCount(); i < tabCount; i++) {
            TabLayout.Tab tabAt = tabLayout.getTabAt(i);
            if (tabAt != null) {
                switch (i) {
                    //首页  默认选中
                    case 0:
                        tabAt.setCustomView(R.layout.main_bootom_home_tab);
                        break;
                    //消息
                    case 1:
                        tabAt.setCustomView(R.layout.main_bootom_message_tab);
                        View customView = tabAt.getCustomView();
                        if (customView != null) {
                            msgCount = customView.findViewById(R.id.main_message_count);
                        }
                        break;
                    //我的
                    case 2:
                        tabAt.setCustomView(R.layout.main_bootom_min_tab);
                        break;
                    default:
                }
            }

            tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    super.onTabSelected(tab);
                    setSelectedTabUi(tab.getCustomView(), tab.getPosition(), true);
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    super.onTabUnselected(tab);
                    setSelectedTabUi(tab.getCustomView(), tab.getPosition(), false);
                }
            });
        }
    }

    /**
     * 设置底部table选中(非选中)界面变化
     *
     * @param customView
     * @param position
     * @param isSelected
     */
    private void setSelectedTabUi(View customView, int position, boolean isSelected) {
        switch (position) {
            case 0: {
                TextView homeTab = customView.findViewById(R.id.main_home);
                if (isSelected) {
                    homeTab.setTextColor(getResources().getColor(R.color.tabTextSelected));
                    Drawable drawable = getResources().getDrawable(R.drawable.home_press);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    homeTab.setCompoundDrawables(null, drawable, null, null);
                } else {
                    homeTab.setTextColor(getResources().getColor(R.color.tabTextSelectedNor));
                    Drawable drawable = getResources().getDrawable(R.drawable.home_normal);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    homeTab.setCompoundDrawables(null, drawable, null, null);
                }
            }
            break;
            case 1: {
                TextView messageTab = customView.findViewById(R.id.main_message);
                if (isSelected) {
                    messageTab.setTextColor(getResources().getColor(R.color.tabTextSelected));
                    Drawable drawable = getResources().getDrawable(R.drawable.message_press);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    messageTab.setCompoundDrawables(null, drawable, null, null);
                } else {
                    messageTab.setTextColor(getResources().getColor(R.color.tabTextSelectedNor));
                    Drawable drawable = getResources().getDrawable(R.drawable.message_normal);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    messageTab.setCompoundDrawables(null, drawable, null, null);
                }
            }
            break;
            case 2: {
                TextView minTab = customView.findViewById(R.id.main_min);
                if (isSelected) {
                    minTab.setTextColor(getResources().getColor(R.color.tabTextSelected));
                    Drawable drawable = getResources().getDrawable(R.drawable.mine_press);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    minTab.setCompoundDrawables(null, drawable, null, null);
                } else {
                    minTab.setTextColor(getResources().getColor(R.color.tabTextSelectedNor));
                    Drawable drawable = getResources().getDrawable(R.drawable.mine_normal);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    minTab.setCompoundDrawables(null, drawable, null, null);
                }
            }
            break;
            default:
        }

    }

    @Override
    public void setUnreadMessage(int totalMsgCount) {
        if (totalMsgCount > 99) {
            msgCount.setVisibility(View.VISIBLE);
            msgCount.setText("99+");
        } else if (totalMsgCount > 0) {
            msgCount.setVisibility(View.VISIBLE);
            msgCount.setText(String.valueOf(totalMsgCount));
        } else {
            msgCount.setVisibility(View.GONE);
        }
    }


}
